import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';
import 'package:pipedream_lighting/src/tool/assets.dart';

class SearchItem extends StatefulWidget {
  final DSPeripheral device;
  bool isManager = false;
  Function() onSelect;
  Function() onPressLongToChangeName;
  SearchItem(
      {super.key,
      required this.device,
      required this.isManager,
      required this.onSelect,
      required this.onPressLongToChangeName});

  @override
  State<SearchItem> createState() => _SearchItemState();
}

class _SearchItemState extends State<SearchItem> {
  bool get isconncet {
    return widget.device.peripheral?.result.device.isConnected ?? false;
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return GestureDetector(
        onTap: widget.isManager ? null : () => widget.onSelect(),
        onLongPress: widget.isManager ? null : () => widget.onPressLongToChangeName(),
        child: Container(
          width: constraints.maxWidth,
          height: 40,
          color: Color_bg_view,
          child: Stack(
            children: [
              ///图标
              Positioned(
                left: 20,
                top: 0,
                bottom: 0,
                child: Center(
                  child: Image.asset(height: 20, isconncet ? assets.light_on : assets.light_off),
                ),
              ),

              ///设备名字
              Positioned(
                left: 50,
                top: 0,
                bottom: 0,
                child: Center(
                  child: Text(
                    widget.device.peripheral?.result.device.platformName ?? '',
                    style: TextStyle(fontSize: 13, color: isconncet ? Color_select_btn : Colors.white),
                  ),
                ),
              ),

              ///设备自定义名字
              Positioned(
                right: 70,
                top: 0,
                bottom: 0,
                child: widget.isManager
                    ? Container(height: 0)
                    : Center(
                        child: Text(
                          widget.device.name,
                          textAlign: TextAlign.right,
                          style: TextStyle(fontSize: 13, color: isconncet ? Color_select_btn : Colors.white),
                        ),
                      ),
              ),

              ///switch开关
              Positioned(
                right: 10,
                top: 6,
                bottom: 6,
                child: Image.asset(
                  widget.isManager
                      ? assets.move
                      : isconncet
                          ? assets.connect_select
                          : assets.disconnect,
                  width: 30,
                ),
              ),

              ///下划线
              Positioned(
                left: 0,
                right: 0,
                bottom: 0,
                child: SizedBox(
                  height: 0.5,
                  child: Container(color: Color_line_view),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }
}
